#ifndef FINDRECTTARGET_H
#define FINDRECTTARGET_H

#include "opencv2/core/core.hpp"
#include "ContourCornerFinder.h"
#include "ExtractLines.h"
#include "RectangleFinder.h"

// searches for a rectangular target in the image
class FindRectTarget
{
public:
    FindRectTarget( int minContourLength , int imgWidth , int imgHeight );
    ~FindRectTarget();

    // processes the image and returns true if the target was found
    bool process( cv::Mat &binaryImg );

    const RectPt& getTarget();

    void setConfig( double lineTol );

    std::vector< std::vector<ContourCorner> >& getCorners();

    void drawMorphological( cv::Mat &img );

    void drawContours( cv::Mat &img );

    void drawCorners( cv::Mat &img );

    void drawLines( cv::Mat &img );

    void drawRectLineSeg( cv::Mat &img );

//    std::vector<ShapeDesc> getEncodedShapes();

private:

    void drawLineSegment( const LineSegmentF &f , const cv::Scalar& color , cv::Mat &img );

    bool selectBestTarget();

    ContourCornerFinder cornerFinder;
    ExtractLines lineFinder;
    RectangleFinder rectFinder;

    int imgWidth;
    int imgHeight;

    cv::Mat tempImg;

    std::vector<std::vector<cv::Point> > contours0;
    std::vector<std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> hierarchy;

    std::vector< std::vector<ContourCorner> > corners;

    std::vector<LineSegmentF> lines;

    int minContourLength;

    // where the target is in the image
    RectPt targetDesc;
};

#endif // FINDRECTTARGET_H
